// pages/chat/chat.js
var app = getApp();
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
  chat:[],//聊天信息
  total:'',
  page:1,
  complete:false,
  act:'addUserChat',//接口
  selectImg:[],//图片
  end:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      options:options
    })
    if(options.id!=undefined){
      that.setData({
        id: options.id
      })
    }
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          height: res.windowHeight
        })
      }
    })
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api,
        data: {
          app: 'member',
          act: 'myDetail',
          t_session: session
        },
        success: function (res) {
          var data = res.data;
          if (data.done && data.retval) {
            that.setData({
              count: data.retval.count,
              member: app.turn(data.retval.member),
              loading: false
            })
            console.log(that.data.member);
            if (that.data.member[0].is_doctor == 1) {
              that.setData({
                complete:true,
                act:'addDoctorChat'
              })
            }
          }
        },
        fail: function (res) {
          that.setData({
            loading: true,
            errorTips: "app:menber连接失败"
          })
        }
      })
    })
    that.getData();
  },
  getData:function(){
    var that = this;
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api,
        data: {
          app: 'member',
          act: 'getChat',
          consult_id: that.data.options.consult_id,
          page:that.data.page,
          t_session: session
        },
        success: function (res) {
          var data = res.data;
          if(data.done&&data.retval){
            if (data.retval.chat.length != 0){
              that.setData({
                chat: data.retval.chat.reverse().concat(that.data.chat),
                total: data.retval.total_page,
                end: data.retval.end_status
              })
            }else{
              that.setData({
                chat:[],
                total: data.retval.total_page,
                end: data.retval.end_status
              })
            }
          }
        },
        fail: function (res) {

        }
      })
    })
  },
  showimg:function(res){
    var arr=[];
    arr.push(res.currentTarget.dataset.img)
    wx.previewImage({
      current: res.currentTarget.dataset.img, // 当前显示图片的http链接
      urls: arr // 需要预览的图片http链接列表
    })
  },
  stopcontent:function(){
    var that = this;
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api,
        data:{
          app:'doctor',
          act:'end_chat',
          id:that.data.id,
          t_session:session,
          consult_id: that.data.options.consult_id
        },
        success:function(res){
          var data =res.data;
          if(data.done&&data.retval){
            wx.showToast({
              title: '话题已结束',
              icon:'success'
            })
          }else{
            wx.showToast({
              title: '请重试',
              icon:'loading'
            })
          }
        }
      })
    })
  },
  
  // //获得焦点
  // input:function(){
  //   var that = this;
  //   if(that.data.id==true){
  //     that.setData({
  //       id:false
  //     })
  //   }else{
  //     that.setData({
  //       id:true
  //     })
  //   }
  // },
  //发送
  show:function(){
    var that = this;
    that.setData({
      box: that.data.box == true ? false : true
    })
  },
  confirm:function(res){
    var that = this;
    that.setData({
      content:res.detail.value
    })
  },
  submit_box: function () {
    var that = this;
    if (that.data.end!=2){
      wx.showToast({
        title: '该聊天已结束',
        icon:'loading'
      })
      return false
    }else{
      if (that.data.content != undefined && that.data.content.length > 0) {
        app.getLoginSession(function (session) {
          wx.request({
            url: app.api,
            data: {
              app: 'doctor',
              act: that.data.act,
              consult_id: that.data.options.consult_id,
              content: that.data.content,
              t_session: session
            },
            success: function (res) {
              var data = res.data;
              if (data.done && data.retval) {
                that.setData({
                  box: that.data.box == true ? false : true,
                  chat: [],
                  page:1,
                  content:''
                })
                that.getData();
              }
              else {
                wx.showModal({
                  title: '提示',
                  content: '发送失败',
                  success: function (res) {
                    if (res.confirm) {
                      that.submit_box();
                    }
                  }
                })
              }
            }
          })
        })
      }
    }
   
    
  },
  // 选择图片
  chooseImg: function () {
    var that = this;
    var selectImg=[]
    that.setData({
      selectImg:[]
    })
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {

        var tempFilePaths = res.tempFilePaths

        if (tempFilePaths.length > 1) {
          that.tips('最多只能上传1张')
        } else {
            selectImg.push(tempFilePaths[0])
          that.setData({
            selectImg: selectImg
          })
          that.uploadImg(that.data.selectImg);
        }

      },
      fail: function () {
        that.tips('选取图片失败')
      }
    })
  },
  // 上传图片
  uploadImg: function (img, fn) {
    var that = this;
    console.log(img);
    app.getLoginSession(function (session) {
      wx.uploadFile({
        url: app.api,
        filePath: img[0],
        name: 'file',
        formData: {
          app: 'member',
          act: 'upload_file',
          file: img[0],
          t_session: session
        },
        method: 'POST',
        header: {
          'content-type': 'multipart/form-data'
        },
        success: function (res) {
          var data = JSON.parse(res.data);
          var rlt = data.retval;
          if (data.done) {
            that.setData({
              content: rlt.save_path
            })
            typeof fn == 'function' && fn();
            console.log(fn)
            that.submit_box();

          } else {
            console.log(data);
            that.tips('图片上传失败')
          }
          console.log(res);
        },
        fail: function () {
          console.log("失败");
        }
      })
    })
  },
  /**
   * 页面上拉拉触底事件的处理函数
   */
  onPullDownRefresh: function () {
    var that = this;
    if (that.data.total > that.data.page){
      that.setData({
        page:parseInt(that.data.page)+1
      })
      that.getData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})